Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

曼陀號領航計畫(1) 報名到錄取

曼陀號領航計畫(1) 報名到錄取

演習課 WEEK14 (部屬)

演習課 WEEK14 (部屬)

解決 git@github.com permission denied (publickey) fatal: Could not read from remote repository.

解決 git@github.com permission denied (publickey) fatal: Could not read from remote repository.






留言討論